<template>
	<view class="policy-info">
		<!-- <u-tabs :list="tabList"  :current="currentIndex" @change="change"></u-tabs> -->
		<!-- <view class="tabs">
			<view :class="['tabs-item',{'active':currentIndex==index}]" v-for="(item,index) in tabList" :key="index" @click="change(index)">
				{{item.name}}
			</view>
		</view> -->
		<view class="policy-info-contain">
				<view class="policy-info-contain-item" v-if="currentIndex == 0">
					<!-- <u-field v-model="form.firstAgentName" label="上级代理商名称" disabled /> -->
					<!-- <u-field v-model="form.firstAgentNo" label="上级代理商编号" disabled /> -->
					<u-field v-model="form.agentName" label="代理商名称" disabled />
					<u-field v-model="form.agentNo" label="代理商编号" disabled />
					<u-field v-model="form.policyName" label="产品政策名称" disabled />
					<u-field v-model="form.policyCode" label="产品政策编码" disabled />
					<u-field v-model="form.addDate" label="开通时间" disabled />
				</view>
				<template v-if="currentIndex == 1">
					<template v-if="form.activeShare">
						<view class="policy-info-contain-item">
							<template v-if="!isEdit">
								<!-- <u-field v-model="form.activeShare.amount" label="奖励金额" disabled /> -->
								<u-field v-model="form.activeShare.shareAmount" label="分账金额" disabled />
							</template>
							<template v-else>
								<!-- <u-field v-model="activeShare.amount" label="奖励金额" disabled /> -->
								<u-field v-model="activeShare.shareAmount"
									:label="`分账金额(${activeShare.minShareAmount}-${activeShare.maxShareAmount})`" />
							</template>
						</view>
						<view class="footer" v-if="!isMyPolicy">
							<u-button hover-class="none" @click="cancel" v-if="isEdit">取消</u-button>
							<u-button hover-class="none" type="primary" @click="submitForm">{{ isEdit ? "提交" : "修改" }}
							</u-button>
						</view>
					</template>
					<template v-else>
						<view>暂未配置</view>
					</template>
				</template>
				<template v-if="currentIndex == 2">
					<template v-if="form.standardShare">
						<template v-if="!isEdit">
							<block v-for="(standardItem, standardIndex) in form.standardShare" :key="secondKey('A',standardIndex)">
								<h4>达标{{ standardIndex + 1 }}</h4>
								<view v-if="standardItem.existShare == 1" class="policy-info-contain-item">
									<!-- <u-field v-model="standardItem.configure.amount" label="奖励金额" disabled /> -->
									<u-field v-model=" standardItem.configure.shareAmount" label="分账金额" disabled />
								</view>
							</block>
						</template>
						<template v-else>
							<block v-for="(standardItem, standardIndex) in form.standardShare" :key="standardIndex">
								<!-- <h4>达标{{ standardIndex + 1 }}</h4> -->
								<view class="policy-info-contain-item">
									<u-field v-model="standardItem.cond" label="达标等级" disabled />
									<template v-if="standardItem.existShare == 1">
										<!-- <u-field v-model="standardItem.configure.amount" label="奖励金额" disabled /> -->
										<u-field v-model="standardItem.configure.shareAmount"
											:label="`分账金额(${standardItem.configure.minShareAmount}-${standardItem.configure.maxShareAmount})`" />
									</template>
								</view>
							</block>
						</template>
						<view class="footer" v-if="!isMyPolicy">
							<u-button hover-class="none" @click="cancel" v-if="isEdit">取消</u-button>
							<u-button hover-class="none" type="primary" @click="submitForm">{{ isEdit ? "提交" : "修改" }}
							</u-button>
						</view>
					</template>
					<template v-else>
						<view>暂未配置</view>
					</template>
				</template>
				<template v-if="currentIndex == 3">
					<template v-if="form.serviceFeeShare">
						<template v-if="!isEdit">
							<block v-for="(serviceFeeItem, serviceFeeIndex) in form.serviceFeeShare"
								:key="secondKey('B',serviceFeeIndex)">
								<h4>服务费{{ serviceFeeIndex + 1 }}</h4>
								<view class="policy-info-contain-item">
									<u-field v-model="serviceFeeItem.amount" label="服务费金额" disabled />
									<u-field v-model="serviceFeeItem.shareAmount" label="分账金额" disabled />
								</view>
							</block>
						</template>
						<template v-else>
							<block v-for="(serviceFeeItem, serviceFeeIndex) in serviceFeeShare"
								:key="secondKey('b',serviceFeeIndex)">
								<!-- <h4>服务费{{ serviceFeeIndex + 1 }}</h4> -->
								<view class="policy-info-contain-item">
									<u-field v-model="serviceFeeItem.amount" label="服务费金额" disabled />
									<u-field v-model="serviceFeeItem.shareAmount"
										:label="`分账金额(${serviceFeeItem.minShareAmount}-${serviceFeeItem.maxShareAmount})`" />
								</view>
							</block>
						</template>
						<view class="footer" v-if="!isMyPolicy">
							<u-button hover-class="none" @click="cancel" v-if="isEdit">取消</u-button>
							<u-button hover-class="none" type="primary" @click="submitForm">{{ isEdit ? "提交" : "修改" }}
							</u-button>
						</view>
					</template>
					<template v-else>
						<view>暂未配置</view>
					</template>
				</template>
				<template v-if="currentIndex == 4">
					<template v-if="form.commFeeShare">
						<template v-if="!isEdit">
							<block v-for="(commFeeItem, commFeeIndex) in form.commFeeShare" :key="secondKey('D',commFeeIndex)">
								<h4>通讯费{{ commFeeIndex + 1 }}</h4>
								<view class="policy-info-contain-item">
									<u-field v-model="commFeeItem.amount" label="通讯费金额" disabled />
									<u-field v-model="commFeeItem.shareAmount" label="分账金额" disabled />
								</view>
							</block>
						</template>
						<template v-else>
							<block v-for="(commFeeItem, commFeeIndex) in commFeeShare" :key="secondKey('C',commFeeIndex)">
								<view class="policy-info-contain-item">
									<u-field v-model="commFeeItem.amount" label="通讯费金额" disabled />
									<u-field v-model="commFeeItem.shareAmount"
										:label="`分账金额(${commFeeItem.minShareAmount}-${commFeeItem.maxShareAmount})`" />
								</view>
							</block>
						</template>
						<view class="footer" v-if="!isMyPolicy">
							<u-button hover-class="none" @click="cancel" v-if="isEdit">取消</u-button>
							<u-button hover-class="none" type="primary" @click="submitForm">{{ isEdit ? "提交" : "修改" }}
							</u-button>
						</view>
					</template>
					<template v-else>
						<view>暂未配置</view>
					</template>
				</template>
				<template v-if="currentIndex == 5">
					<template v-if="!isEdit">
						<block v-for="(agentCostItem, agentCostIndex) in form.agentCostScope" :key="secondKey('D',agentCostIndex)">
							<h4>{{totalTypeFormat(agentCostItem.totalType) }}</h4>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 1">
								<u-field v-model="agentCostRate.posCreditRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.posCreditAdd"
									:label="`笔数费(元)`" disabled />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 2">
								<u-field v-model="agentCostRate.posDebitRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.posDebitAdd"
									:label="`笔数费(元)`" disabled />
								<u-field v-if="agentCostItem.existLimit == 1" v-model="agentCostRate.posDebitLimit"
									:label="`笔数费(元)`" disabled />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 3">
								<u-field v-model="agentCostRate.ysfRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.ysfAdd" :label="`笔数费(元)`"
									disabled />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 4">
								<u-field v-model="agentCostRate.scanfRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.scanfAdd"
									:label="`笔数费(元)`" disabled />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 5">
								<u-field v-model="agentCostRate.vipCreditRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.vipCreditAdd"
									:label="`笔数费(元)`" disabled />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 6">
								<u-field v-model="agentCostRate.vipDebitRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.vipDebitAdd"
									:label="`笔数费(元)`" disabled />
								<u-field v-if="agentCostItem.existLimit == 1" v-model="agentCostRate.vipDebitLimit"
									:label="`笔数费(元)`" disabled />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 7">
								<u-field v-model="agentCostRate.qpayRate" :label="`费率(%)`" disabled />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.qpayAdd"
									:label="`笔数费(元)`" disabled />
							</view>
						</block>
					</template>
					<template v-else>
						<block v-for="(agentCostItem, agentCostIndex) in form.agentCostScope" :key="secondKey('d',agentCostIndex)">
							<h4>{{totalTypeFormat(agentCostItem.totalType) }}</h4>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 1">
								<u-field v-model="agentCostRate.posCreditRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.posCreditAdd"
									:label="`笔数费(元)`" />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 2">
								<u-field v-model="agentCostRate.posDebitRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.posDebitAdd"
									:label="`笔数费(元)`" />
								<u-field v-if="agentCostItem.existLimit == 1" v-model="agentCostRate.posDebitLimit"
									:label="`笔数费(元)`" />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 3">
								<u-field v-model="agentCostRate.ysfRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.ysfAdd"
									:label="`笔数费(元)`" />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 4">
								<u-field v-model="agentCostRate.scanfRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.scanfAdd"
									:label="`笔数费(元)`" />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 5">
								<u-field v-model="agentCostRate.vipCreditRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.vipCreditAdd"
									:label="`笔数费(元)`" />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 6">
								<u-field v-model="agentCostRate.vipDebitRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.vipDebitAdd"
									:label="`笔数费(元)`" />
								<u-field v-if="agentCostItem.existLimit == 1" v-model="agentCostRate.vipDebitLimit"
									:label="`笔数费(元)`" />
							</view>
							<view class="policy-info-contain-item" v-if="agentCostItem.totalType == 7">
								<u-field v-model="agentCostRate.qpayRate" :label="`费率(%)`" />
								<u-field v-if="agentCostItem.existAdd == 1" v-model="agentCostRate.qpayAdd"
									:label="`笔数费(元)`" />
							</view>
						</block>
					</template>
					<view class="footer" v-if="!isMyPolicy">
						<u-button hover-class="none" @click="cancel" v-if="isEdit">取消</u-button>
						<u-button hover-class="none" type="primary" @click="submitForm">{{ isEdit ? "提交" : "修改" }}
						</u-button>
					</view>
				</template>
			</view>
		
	</view>
</template>

<script>
	import {
		myPolicyList,
		listDataUnderAgent,
		activeShareEdit,
		agentCostEdit,
		standardShareEdit,
		serviceFeeShareEdit,
		commFeeShareEdit,
	} from "@/api/agent-open";
	export default {
		data() {
			return {
				queryParams: {
					pageNum: 1,
					pageSize: 10,
				},
				form: {},
				tabList: [{
						name: "关联产品"
					},
					{
						name: "激活政策"
					},
					{
						name: "达标政策"
					},
					{
						name: "服务费返现"
					},
					{
						name: "通讯费"
					},
					{
						name: "结算成本"
					},
				],
				currentIndex: 0,
				totalTypeOptions: [{
						"dictLabel": "POS贷记卡",
						"dictValue": "1",
					},
					{
						"dictLabel": "POS借记卡",
						"dictValue": "2",
					},
					{
						"dictLabel": "云闪付",
						"dictValue": "3",
					},
					{
						"dictLabel": "扫码",
						"dictValue": "4",
					},
					{
						"dictLabel": "VIP贷记卡",
						"dictValue": "5",
					},
					{
						"dictLabel": "VIP借记卡",
						"dictValue": "6",
					},
					{
						"dictLabel": "快捷",
						"dictValue": "7",
					}
				],
				activeShare: {},
				standardShare: [],
				serviceFeeShare: [],
				commFeeShare: [],
				agentCostRate: {},
				isEdit: false,
				isMyPolicy: false,
				agentCostRateRule: [], //代理商结算成本必填信息
			}
		},
		onLoad(options) {
			this.queryParams.agentNo = options.agentNo;
			this.queryParams.policyCode = options.policyCode;
			this.isMyPolicy = options.type == 0 ? true : false; // type 0:自己 ，1直属
			this.handleUpdate();
		},
		methods: {
			change(index) {
				console.log({index})
				this.currentIndex = index;
				this.isEdit = false;
			},
			async handleUpdate(query) {
				let response;
				if (this.isMyPolicy) {
					response = await myPolicyList(this.queryParams)
				} else {
					response = await listDataUnderAgent(this.queryParams)
				}
				this.form = response.rows[0];
				this.activeShare = this.form.activeShare;
				this.standardShare = this.form.standardShare;
				this.serviceFeeShare = this.form.serviceFeeShare;
				this.commFeeShare = this.form.commFeeShare;
				this.agentCostRate = this.form.agentCostRate;
				const obj = Object.assign({}, this.form.agentCostRate)
				this.agentCostRateRule = Object.getOwnPropertyNames(obj);
				const that = this;
				// 获取有值的key 
				Object.keys(that.form.agentCostRate).forEach((key)=> {
					if(that.form.agentCostRate[key]){
						that.agentCostRateRule.push(key);
					}
				})
			},
			// 代理商结算成本必填信 匹配
			agentCostRateRulesInfo(params) {
				let isHas = true;
				let message = "";
				//  没有值时错误信息
				const errorInfo = {
					posCreditRate: "请填写POS贷记卡费率",
					posCreditAdd: "请填写POS贷记卡笔数费",
					posDebitRate: "请填写POS借记卡费率",
					posDebitAdd: "请填写POS借记卡笔数费",
					posDebitLimit: "请填写POS借记卡封顶手续费",
					vipCreditRate: "请填写VIP贷记卡费率",
					vipCreditAdd: "请填写VIP贷记卡笔数费",
					vipDebitRate: "请填写VIP借记卡费率",
					vipDebitAdd: "请填写VIP借记卡笔数费",
					vipDebitLimit: "请填写VIP借记卡封顶手续费",
					ysfRate: "请填写云闪付费率",
					ysfAdd: "请填写云闪付笔数费",
					scanfRate: "请填写扫码费率",
					scanfAdd: "请填写扫码笔数费",
					qpayRate: "请填写快捷支付费率",
					qpayAdd: "请填写快捷支付笔数费",
				};
				// 需要有的值
				const formCheckArray = [...this.agentCostRateRule];
				// 查找没有值的返回改参数
				const result = formCheckArray.find((key) => !params[key]);
				if (result) { // 没有输入值时的提示判断
					message = errorInfo[result];
				}
				if (message) {
					this.$tip.toast(message)
					isHas = false;
				}
				return isHas;
			},
			// 修改提交
			async submitForm() {
				let agentInfo = {
					agentNo: this.queryParams.agentNo,
					policyCode: this.queryParams.policyCode,
				};
				if (!this.isEdit) {
					return (this.isEdit = true);
				}
				let params = {};
				switch (Number(this.currentIndex)) {
					case 1:
						params = {
							...agentInfo,
							activeShare: {
								...this.activeShare
							}
						};
						break;
					case 2:
						params = {
							...agentInfo,
							standardShare: [...this.standardShare]
						};
						break;
					case 3:
						params = {
							...agentInfo,
							serviceFeeShare: [...this.serviceFeeShare]
						};
						break;
					case 4:
						params = {
							...agentInfo,
							commFeeShare: [...this.commFeeShare]
						};
						break;
					case 5:
						params = {
							...agentInfo,
							agentCostRate: {
								...this.agentCostRate
							}
						};
						break;
					default:
						break;
				}
				// this.isEdit = false;
				let res;
				switch (Number(this.currentIndex)) {
					case 1: // 激活
						res = await activeShareEdit(params);
						break;
					case 2: // 达标
						res = await standardShareEdit(params);
						break;
					case 3: // 服务费
						res = await serviceFeeShareEdit(params);
						break;
					case 4: // 通讯费
						res = await commFeeShareEdit(params);
						break;
					case 5: // 结算成本
						let isHas = this.agentCostRateRulesInfo(params.agentCostRate);
						if (isHas) res = await agentCostEdit(params);
						break;
				}
				if (!res) return;
				const {
					code,
					data,
					msg
				} = res;
				if (code === 200) {
					this.$tip.toast(msg)
					this.handleUpdate();
				}
				this.isEdit = false

			},
			/** 取消按钮 **/
			cancel() {
				this.isEdit = false;
			},
			selectDictLabel(datas, value) {
				var actions = [];
				Object.keys(datas).map((key) => {
					if (datas[key].dictValue == ('' + value)) {
						actions.push(datas[key].dictLabel);
						return false;
					}
				})
				return actions.join('');
			},
			totalTypeFormat(row, column) {
				return this.selectDictLabel(this.totalTypeOptions, row);
			},
			secondKey(first, second){
			    return `${first}_${second}`  
			}, 
		}
	}
</script>

<style lang="scss">
	.policy-info-contain {
		margin: R(20);

		&-item {
			background-color: #FFFFFF;
			border-radius: R(10);
			padding: R(20);
			margin: R(20) 0;
		}

		.footer {
			display: flex;
			justify-content: space-around;
			margin: R(20) 0;

			button {
				width: R(150);
				height: R(70);
				line-height: R(70);
			}
		}
	}
	.tabs{
		display: flex;
		justify-content: space-between;
		align-items: center;
		text-align: center;
		flex-wrap: wrap;
		background-color: #FFFFFF;
		&-item{
		    height: R(70px);
			width: 30%;
			line-height: R(70px);
		}
		.active{
			background-color: #188dd5;
			color: #FFFFFF;
		}
	}

	/deep/ .u-label {
		flex: 0 0 R(250) !important;
		color: #8C8C8C;
	}

	/deep/ .u-border-bottom:after {
		border-bottom-width: 0px;
	}

	/deep/ .uni-input-input {
		text-align: right;
	}

	/deep/ .uni-input-input:disabled {
		color: #8C8C8C;
	}
</style>
